package com.sskj.api.controller.admin;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sskj.api.domain.Project;
import com.sskj.api.domain.ProjectDevice;
import com.sskj.api.domain.ProjectMember;
import com.sskj.api.service.IProjectDeviceService;
import com.sskj.api.service.IProjectMemberService;
import com.sskj.api.service.IProjectService;
import com.sskj.api.vo.req.PageReq;
import com.sskj.api.vo.req.ProjectDevicePageListReq;
import com.sskj.api.vo.req.ProjectMemberPageListReq;
import com.sskj.api.vo.req.ProjectPageListReq;
import com.sskj.common.util.ApiResponseUtil;
import com.sskj.common.vo.ApiResponse;
import com.sskj.common.vo.SimplePagination;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "项目管理")
@RestController
@RequestMapping("/admin/project")
public class ProjectController {

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IProjectMemberService projectMemberService;

    @Autowired
    private IProjectDeviceService projectDeviceService;

    @Operation(summary = "新增项目")
    @PostMapping("add")
    public ApiResponse<Object> add(@RequestBody Project project) {
        projectService.add(project);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "删除项目")
    @PostMapping("deleteById")
    public ApiResponse<Object> deleteById(@RequestParam Long id) {
        projectService.deleteById(id);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "更新项目")
    @PostMapping("update")
    public ApiResponse<Object> update(@RequestBody Project project) {
        projectService.update(project);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "项目详情")
    @GetMapping("detail")
    public ApiResponse<Project> detail(Long id) {
        Project detail = projectService.detail(id);

        return ApiResponseUtil.success(detail);
    }

    @Operation(summary = "分页查询")
    @GetMapping("pageList")
    public ApiResponse<SimplePagination<Project>> pageList(ProjectPageListReq req) {
        IPage<Project> projectIPage = projectService.pageList(req);

        return ApiResponseUtil.success(projectIPage);
    }

    @Operation(summary = "新增项目组员")
    @PostMapping("memberAdd")
    public ApiResponse<Object> memberAdd(@Valid @RequestBody ProjectMember projectMember) {
        projectMemberService.add(projectMember);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "修改项目组员")
    @PostMapping("memberUpdate")
    public ApiResponse<Object> memberUpdate(@Valid @RequestBody ProjectMember projectMember) {
        projectMemberService.update(projectMember);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "移除项目组员")
    @PostMapping("memberDeleteById")
    public ApiResponse<Object> memberDeleteById(@RequestParam Long id) {
        projectMemberService.deleteById(id);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "项目组员分页列表")
    @GetMapping("memberPageList")
    public ApiResponse<SimplePagination<ProjectMember>> memberPageList(ProjectMemberPageListReq req) {
        IPage<ProjectMember> projectMemberIPage = projectMemberService.pageList(req);

        return ApiResponseUtil.success(projectMemberIPage);
    }

    @Operation(summary = "我的项目列表")
    @GetMapping("myProjects")
    public ApiResponse<SimplePagination<ProjectMember>> myProjects(PageReq req) {
        long userId = StpUtil.getLoginIdAsLong();
        ProjectMemberPageListReq projectMemberPageListReq = new ProjectMemberPageListReq();
        projectMemberPageListReq.setProjectId(null);
        projectMemberPageListReq.setUserId(userId);
        projectMemberPageListReq.setPageNum(req.getPageNum());
        projectMemberPageListReq.setPageSize(req.getPageSize());
        IPage<ProjectMember> projectMemberIPage = projectMemberService.pageList(projectMemberPageListReq);

        return ApiResponseUtil.success(projectMemberIPage);
    }

    @Operation(summary = "新增关联设备")
    @PostMapping("deviceAdd")
    public ApiResponse<Object> deviceAdd(@Valid @RequestBody ProjectDevice projectDevice) {
        projectDeviceService.add(projectDevice);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "删除关联设备")
    @PostMapping("deviceDeleteById")
    public ApiResponse<Object> deviceDeleteById(@RequestParam Long id) {
        projectDeviceService.deleteById(id);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "设备分页列表")
    @GetMapping("devicePageList")
    public ApiResponse<SimplePagination<ProjectDevice>> devicePageList(ProjectDevicePageListReq req) {
        IPage<ProjectDevice> projectDeviceIPage = projectDeviceService.pageList(req);

        return ApiResponseUtil.success(projectDeviceIPage);
    }
}
